package com.xzl.xep.service.impl;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xzl.xep.common.utils.DateUtil;
import com.xzl.xep.dao.BusinessDao;
import com.xzl.xep.exception.BusinessException;
import com.xzl.xep.pojo.Company;
import com.xzl.xep.pojo.Customer;
import com.xzl.xep.pojo.Operator;
import com.xzl.xep.pojo.Payment;
import com.xzl.xep.pojo.Product;
import com.xzl.xep.pojo.Sale;
import com.xzl.xep.pojo.SaleDetail;
import com.xzl.xep.service.Order;
import com.xzl.xep.service.OrderService;

@Service
public class OrderServiceImpl extends BaseServiceImpl implements OrderService {

	@Autowired
	private BusinessDao business;
	
	@Override
	public Sale sale(Order order, Operator operator) {
		Customer customer = null;
		
		if(order.hasCustomer()){
			 customer = this.loadObject(Customer.class, order.getCustomerId());
			 if(order.hasCredit())
				 customer =business.credit(customer, order.getTotalMoney(), order.getWay(), operator);
			 
			 customer.setLastSpendTime(DateUtil.getCurrentDate());
			 customer.setTotalSpend(customer.getTotalSpend()+order.getTotalMoney());
			 if(customer.getBalance() < order.getTotalMoney())
				 throw new BusinessException("余额不足！");
			 
			 customer.setBalance(customer.getBalance() - order.getTotalMoney());
			 dao.updateObject(customer);
			 
			
		}
			
		
		
		
		Map<String, Integer> map = order.getDetail();
		Set<SaleDetail> details = new HashSet<SaleDetail>();
		
		Double totalmoney = 0d;
		

		Sale sale = order.getSale(operator, customer);
		
		if(sale.getPayment().getWay().equals(Payment.CASH_WAY)){
			Company company = dao.loadObject(Company.class, sale.getCompanyId());
			company.plusCash(sale.getPayMoney());
			dao.updateObject(company);
		}
		
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
			Product product = this.getProduct(operator.getCompanyId(), entry.getKey());
			totalmoney += product.getPrice() * entry.getValue();
			product.sale(entry.getValue());
			dao.updateObject(product);
			
			SaleDetail detail = new SaleDetail();
			detail.setCounts(entry.getValue());
			detail.setCreateTime(DateUtil.getCurrentDate());
			detail.setModifyTime(DateUtil.getCurrentDate());
			detail.setMoney(product.getPrice() * entry.getValue());
			detail.setProductId(product.getId());
			detail.setProductName(product.getName());
			detail.setProductPrice(product.getPrice());
			detail.setSale(sale);
			details.add(detail);
		}
		
		sale.setDetails(details);
		
		if(!totalmoney.equals(sale.getSaleMoney()))
			throw new BusinessException("金额错误！");
		
		dao.saveObject(sale);
		return sale;
	}

	private Product getProduct(Long companyId,String key) {
		StringBuffer sb = new StringBuffer();
		sb.append("from Product where code = '").append(key).append("' and companyId = ").append(companyId);
		
		return dao.selectOneByHql(sb.toString());
	}

	@Override
	public Sale getSale(Long saleId) {
		
		Sale s =  dao.loadObject(Sale.class, saleId);
		Iterator<SaleDetail>  iterator =  s.getDetails().iterator();
		while(iterator.hasNext()){
			iterator.next();
		}
		
		return s;
	}

}
